package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.MapGeolocationPresenter;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.maps.gwt.client.GoogleMap;
import com.google.maps.gwt.client.LatLng;
import com.google.maps.gwt.client.MapOptions;
import com.google.maps.gwt.client.MapTypeId;
import com.google.maps.gwt.client.Marker;
import com.google.maps.gwt.client.MarkerOptions;

public class MapGeolocationView extends Composite implements MapGeolocationPresenter.Display {
	private VerticalPanel verticalPanel;
	private int heightCPanel;
	public MapGeolocationView() {
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		heightCPanel = CloudMobile.get().getHeightPanelCenter() - 40;
		initHPanelTitle();
		initMap();
	}

	

	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-hPanelCenter");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setSize("100%", "40px");
	    verticalPanel.add(horizontalPanel);  
	    verticalPanel.setCellHeight(horizontalPanel, "40px");
	    verticalPanel.setCellWidth(horizontalPanel, "100%");
	    
	}
	@Override
	public void updatePosM(double latitud,double longitud,String title) {
		LatLng myLatLng = LatLng.create(latitud, longitud);
		CloudMobile.get().getTheMap().setCenter(myLatLng);
		CloudMobile.get().getMarker().setTitle(title);
		CloudMobile.get().getMarker().setPosition(myLatLng);
		
	}

	public void initMap(){
		LatLng myLatLng = LatLng.create(-54.648413, -65.165405);
		if(CloudMobile.get().getPanelMap() == null){
			SimplePanel sPanel = new SimplePanel();
			sPanel.setSize("100%-1px", heightCPanel+"px");
			CloudMobile.get().setPanelMap(sPanel);			
			MapOptions myOptions = MapOptions.create();
			myOptions.setZoom(16.0);
			myOptions.setCenter(myLatLng);
			myOptions.setMapTypeId(MapTypeId.ROADMAP);
			CloudMobile.get().setTheMap(GoogleMap.create( sPanel.getElement(), myOptions )) ;		
			MarkerOptions newMarkerOpts = MarkerOptions.create();
			newMarkerOpts.setPosition(myLatLng);
			newMarkerOpts.setMap(CloudMobile.get().getTheMap());
			CloudMobile.get().setMarker(Marker.create(newMarkerOpts));
		}
		else{
			GoogleMap nulo = null;
			CloudMobile.get().getMarker().setMap(nulo);
			MarkerOptions newMarkerOpts = MarkerOptions.create();
			newMarkerOpts.setPosition(myLatLng);
			newMarkerOpts.setMap(CloudMobile.get().getTheMap());
			CloudMobile.get().setMarker(Marker.create(newMarkerOpts));
			
		}
	    verticalPanel.add(CloudMobile.get().getPanelMap());
	}


}
